/**
 * 
 */
package com.nari.baseinfo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.ICityDao;
import com.nari.baseinfo.model.ICity;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.platform.components.dhtmltree.DhtmlTreeNode;
import com.nari.platform.components.dhtmltree.TreeUserData;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.pinyin.main.PinyinTranslateSolution;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class CityServiceImpl extends GenericServiceImpl<ICity, String> 
		implements ICityService {
	private String cityIcon;
	
	/**
	 * 查询所有一级行政机构
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public List<ICity> queryFirstLevelCity() throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.queryFirstLevelCity();
	}
	
	/**
	 * 根据父机构查询所有子机构列表
	 * @param parentID 父机构(可选)，若不存在，则查询一级行政机构
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public List<ICity> queryCityByParentID(String parentID) throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.queryCityByParentID(parentID);
	}
	
	/**
	 * 分页查询行政机构列表
	 * @param parentID 父机构(可选)，若不存在，则查询一级行政机构
	 * @param cityName 行政机构名称(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public ResultWithPageCond<ICity> queryCityPageByParentID(String parentID, String cityName, 
			PageCond pageCond) throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.queryCityPageByParentID(parentID, cityName, pageCond);
	}
	
	/**
	 * 将城市列表转化为树节点
	 * @param cityList
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-23
	 */
	public List<DhtmlTreeNode> formatCityListToTreeNodes(List<ICity> cityList) throws Exception {
		List<DhtmlTreeNode> cityNodes = new ArrayList<DhtmlTreeNode>();
		if(cityList!=null && cityList.size()>0) {
			for(ICity city: cityList) {
				DhtmlTreeNode node = new DhtmlTreeNode();
				node.setId(city.getId());
				node.setText(city.getCityName());
				node.setIm0(this.cityIcon);
				node.setIm1(this.cityIcon);
				node.setIm2(this.cityIcon);
				//设置菜单节点链接
				node.getUserdata().add(new TreeUserData("cityLevel", city.getCityLevel().toString())); 
				node.getItem().add(new DhtmlTreeNode("1", "1"));
				cityNodes.add(node);
			}
		}
		return cityNodes;
	}
	
	/**
	 * 行政机构新增或修改
	 * @param city
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public ResponseMsg saveOrUpdateCity(ICity city) throws Exception {
		//设置行政区划名称拼音全拼
		String cityNamePinyin = PinyinTranslateSolution.fullTranslateChinese(city.getCityName(), "");
		city.setCityNamePinyin(cityNamePinyin);
		//父机构信息、行政级别信息
		String parentAdminSeq = null;
		if(city.getParentID()==null || "".equals(city.getParentID().trim())) {
			city.setParentID(null);
			city.setCityLevel(1);
		} else {
			ICity parentCity = this.getiGenericDao().queryEntityById(city.getParentID());
			city.setCityLevel(parentCity.getCityLevel()+1);
			city.setParentID(parentCity.getId());
			city.setParentCode(parentCity.getAdminCode());
			//父机构区划序列
			parentAdminSeq = parentCity.getAdminSeq();
		}
		if(city.getId()!=null && !"".equals(city.getId().trim())) { //机构修改
			this.getiGenericDao().updateEntity(city, city.getId());
		} else { //机构新增
			this.saveEntity(city);
		}
		//设置区划序列
		if(parentAdminSeq != null) {
			city.setAdminSeq(parentAdminSeq + "." + city.getId());
		} else {
			city.setAdminSeq(city.getId());
		}
		this.getiGenericDao().updateEntity(city, city.getId());
		//设置子节点的对应信息
		List<ICity> childCityList = this.queryCityByParentID(city.getId());
		if(childCityList!=null && childCityList.size()>0) {
			for(ICity childCity: childCityList) {
				childCity.setParentCode(city.getAdminCode());
				this.updateEntity(childCity, childCity.getId());
			}
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 根据父机构唯一标识等信息统计机构个数
	 * @param parentID 父机构唯一标识(可选)，若不存，则统计所有行政机构的个数
	 * @param cityName 机构名称(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-5
	 */
	public int countCityByParent(String parentID, String cityName) throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.countCityByParent(parentID, cityName);
	}
	
	/**
	 * 通过机构ID列表查询机构信息
	 * @param idList 机构ID列表，若列表为空，则返回空列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public List<ICity> queryCityByIDList(List<String> idList) throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.queryCityByIDList(idList);
	}
	
	/**
	 * 通过机构ID列表查询机构ID-机构名称映射表
	 * @param idList 机构ID列表，若列表为空，则返回一个空映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public Map<String, String> queryIdNameMapByIDList(List<String> idList) throws Exception {
		ICityDao iCityDao = (ICityDao) this.getiGenericDao();
		return iCityDao.queryIdNameMapByIDList(idList);
	}

	/**
	 * @return the cityIcon
	 */
	public String getCityIcon() {
		return cityIcon;
	}

	/**
	 * @param cityIcon the cityIcon to set
	 */
	public void setCityIcon(String cityIcon) {
		this.cityIcon = cityIcon;
	}

}
